package calendar;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import CalendarComponents.FullDayView;
import CalendarComponents.LargeDayView;
import CalendarComponents.MenuBar;
import CalendarComponents.SmallDayView;
import Managers.CalendarManager;
import Managers.Day;

@SuppressWarnings("serial")
public class MainWindow extends JFrame {
	private static JTabbedPane viewChanger;
	private static JTabbedPane monthTabs;
	private JButton prevd;
	private JButton nextd;
	private JButton prevw;
	private JButton nextw;
	private JButton prevy;
	private JButton nexty;
	private static JFrame thisWindow = null;
	
	public static JFrame getMainWindow() {
		return thisWindow;
	}
	
	public static JTabbedPane getTabPane() {
		return viewChanger;
	}
	public static JTabbedPane getMonthsTabPane() {
		return monthTabs;
	}

	public MainWindow() {
		try
		{
			//Image borrowed from the  web based outlook client
			this.setIconImage(ImageIO.read(new File("appt.gif")));
		}
		catch (IOException e)
		{
			//fail silently  doesn't matter much it's non critical.
		}
		thisWindow = this;
		this.setResizable(false);
		setTitle("Calendar");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel main = new MainPanel();
		MenuBar menu = new MenuBar();
		setJMenuBar(menu.createMenuBar());
        setContentPane(main);
		
		pack();
		setVisible(true);
	}
	
	private class MainPanel extends JPanel {
		public MainPanel() {
			viewChanger = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
			viewChanger.addTab("Day", new DayPanel());
			viewChanger.addTab("Week", new WeekPanel());
			viewChanger.addTab("Month", new MonthPanel());
			viewChanger.setPreferredSize(new Dimension(1024, 900));
			viewChanger.addChangeListener(new ChangeListener(){

				@Override
				public void stateChanged(ChangeEvent arg0) {
					JTabbedPane pane = (JTabbedPane)arg0.getSource();
					if(pane.getSelectedIndex() == 2)
					{
						monthTabs.setSelectedIndex(CalendarManager.getDay().getDate()[1]);
					}
				}
				
			});
			add(viewChanger);
		}
	}
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new MainWindow(); 
			}
		});
	}
	
	//DAY VIEW
	private class DayPanel extends JPanel {
		public DayPanel()
		{
			JPanel bp = new JPanel();
			bp.setLayout(new BoxLayout(bp, BoxLayout.Y_AXIS));
			
			prevd = new JButton("PREV");
			nextd = new JButton("NEXT");
			
			DayChanger dc = new DayChanger();
			prevd.addActionListener(dc);
			nextd.addActionListener(dc);
			
			bp.add(prevd);
			bp.add(nextd);
			
			setLayout(new BorderLayout());
			setPreferredSize(new Dimension(1024, 768));
			add(new FullDayView(), BorderLayout.WEST);
			add(bp, BorderLayout.EAST);
		}
	}
	
	private class DayChanger implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(prevd)) {
				
				CalendarManager.DeclineDay();
			} else if(e.getSource().equals(nextd)) {
				
				CalendarManager.AdvanceDay();
			}
		}
	}
	
	//WEEK VIEW
	private class WeekPanel extends JPanel {
		public WeekPanel() {
			JPanel jp = new JPanel();
			
			jp.setLayout(new GridLayout(2,4));

			
			//get a week based on the curently pointed too day
			Day[] week = CalendarManager.getWeek();
			
			//add the week while also defining what order they should come in.
			// the second parameter of creating a Dayview is a total hack in the absense
			//of a DayView Grid...
			for(int i = 0; i < 7; i++)
				jp.add(new LargeDayView(week[i], i));
			
			JPanel bp = new JPanel();
			bp.setLayout(new BoxLayout(bp, BoxLayout.Y_AXIS));
			prevw = new JButton("PREV");
			nextw = new JButton("NEXT");
			WeekChanger wc = new WeekChanger();
			prevw.addActionListener(wc);
			nextw.addActionListener(wc);
			bp.add(prevw);
			bp.add(nextw);
			
			setLayout(new BorderLayout());
			setPreferredSize(new Dimension(1024, 768));
			add(jp, BorderLayout.WEST);
			add(bp, BorderLayout.EAST);
		}
	}
	
	private class WeekChanger implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(prevw)) {
				
				CalendarManager.DeclineWeek();
			} else if(e.getSource().equals(nextw)) {
				
				CalendarManager.AdvanceWeek();
			}
		}
	}
	
	//MONTH VIEW
	private class MonthPanel extends JPanel
	{
		public MonthPanel() {

			//get the year
			Day[][] year = CalendarManager.getYear();
			
			monthTabs = new JTabbedPane(JTabbedPane.RIGHT, JTabbedPane.SCROLL_TAB_LAYOUT);
			//for the length of the year
			for(int x = 0; x < year.length; x++)
			{
				JPanel monthviews = new JPanel();
				monthviews.setLayout(new GridLayout(0, 7));
				//for each month
				for(int i = 0; i < year[x].length; i++)
				{  
					//add a small day view based on a day. this also uses the nasty hack in the absense of a dayview grid box
					monthviews.add(new SmallDayView(year[x][i], i, x));
				}
				
				if(x == 1 && !CalendarManager.isLeap(CalendarManager.getYearNumber()))
				{
					SmallDayView sdv = new SmallDayView(null, year[x].length, x, true);
					sdv.setVisible(false);
					monthviews.add(sdv);
				}
				
				monthTabs.addTab(CalendarManager.getMonthName(x), monthviews);
			}
			
			monthTabs.setPreferredSize(new Dimension(1024, 768));	
			add(monthTabs);
			
			prevy = new JButton("PREV YEAR");
			nexty = new JButton("NEXT YEAR");
			YearChanger yc = new YearChanger();
			prevy.addActionListener(yc);
			nexty.addActionListener(yc);
			add(prevy);
			add(nexty);
		}
	}
	
	private class YearChanger implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(prevy)) {
				CalendarManager.DeclineYear();
			}
			else if(e.getSource().equals(nexty)) {
				CalendarManager.AdvanceYear();
			}
		}
	}
}
